package com.pybox.repository;

import com.pybox.entity.TUserGroupObjects;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* 描述：TOptionLogs Repository
* @author wuyingkai
* @date 2018-09-15
*/

@Repository
public interface TUserGroupObjectsRepository extends JpaRepository<TUserGroupObjects, Integer>, JpaSpecificationExecutor<TUserGroupObjects>   {

    @Modifying
    @Query(value = "DELETE FROM  t_user_group_objects WHERE  group_Id =? and user_Id  is not null",nativeQuery = true)
    Integer deleteByGroupId(Integer groupId);

    @Modifying
    @Query(value = "insert  into t_user_group_objects (group_id,user_id) VALUES (?1,?2)",nativeQuery = true)
    Integer addUserIdsByGroupId(Integer groupId,Integer userId);

    @Query(value = "select user_Id from t_user_group_objects where group_Id = ? and user_Id is not null ",nativeQuery = true)
    List<Integer> getUserIdsByGroupId(Integer groupId);

    @Query(value = "select user_Id from t_user_group_objects where group_Id <> ?1 and user_Id is not null ", nativeQuery = true)
    List<Integer> getOtherUserIdsByGroupId(Integer groupId);

    @Modifying
    @Query(value = "DELETE FROM  t_user_group_objects WHERE  group_Id =? and object_Id is not null",nativeQuery = true)
    Integer deleteObjectIdByGroupId(Integer groupId);

    @Modifying
    @Query(value = "insert  into t_user_group_objects (group_Id,object_Id) VALUES (?1,?2)",nativeQuery = true)
    Integer addObjectIdsByGroupId(Integer groupId,Integer objectId);

    @Query(value = "select object_Id from t_user_group_objects where group_Id = ?1 and object_id is not null ",nativeQuery = true)
    List<Integer> getObjectIdsByGroupId(Integer groupId);

    @Query(value = "select object_Id from t_user_group_objects where group_Id <> ?1 and object_id is not null ",nativeQuery = true)
    List<Integer> getOtherObjectIdsByGroupId(Integer groupId);

    @Modifying
    @Query(value = "DELETE FROM  t_user_group_objects WHERE  user_Id =? and object_Id  is not null",nativeQuery = true)
    Integer deleteObjectIdsByUserId(Integer userId);

    @Modifying
    @Query(value = "insert  into t_user_group_objects (user_Id,group_Id) VALUES (?1,?2)",nativeQuery = true)
    Integer addGroupIdByUserId(Integer userId,Integer groupId);

    @Query(value = "select group_Id from t_user_group_objects where User_Id = ? and group_id is not null ",nativeQuery = true)
    List<Integer> getGroupIdsByUserId(Integer userId);


    @Modifying
    @Query(value = "DELETE FROM  t_user_group_objects WHERE  user_Id =? and group_Id  is not null", nativeQuery = true)
    Integer deleteGroupIdsByUserId(Integer userId);

    @Modifying
    @Query(value = "insert  into t_user_group_objects (user_Id,object_Id) VALUES (?1,?2)", nativeQuery = true)
    Integer addObjectIdsByUserId(Integer userId,Integer objectId);

    @Query(value = "SELECT distinct object_id FROM t_user_group_objects WHERE group_id  IN ( SELECT group_id FROM t_user_group_objects t WHERE t.user_id =?1 )AND object_id IS NOT NULL ",nativeQuery = true)
    List<Integer> getObjectIdsByUserId(Integer userId);

    @Modifying
    @Query(value = "DELETE FROM  t_user_group_objects WHERE  object_Id =? and userId  is not null", nativeQuery = true)
    Integer deleteUserIdsByObjectId(Integer objectId);

    @Modifying
    @Query(value = "insert  into t_user_group_objects (userId,objectId) VALUES (?1,?2)", nativeQuery = true)
    Integer addUserIdByObjectId(Integer userId,Integer objectId);

    @Query(value = "SELECT distinct user_id FROM t_user_group_objects WHERE group_id  IN ( SELECT group_id FROM t_user_group_objects t WHERE t.object_id =?1 )AND user_id IS NOT NULL",nativeQuery = true)
    List<Integer> getUserIdsByObjectId(Integer ObjectId);

    @Modifying
    @Query(value = "DELETE FROM  t_user_group_objects WHERE  object_Id =? and group_Id is not null", nativeQuery = true)
    Integer deleteGroupIdsByObjectId(Integer objectId);

    @Query(value = "select group_Id from t_user_group_objects where object_Id = ?1 and group_id is not null ",nativeQuery = true)
    List<Integer> getGroupIdsByObjectId(Integer userId);
}